package com.shine.level.easy;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**有效的括号
 * @author zhangshao
 * @date 2021/6/30 4:49 下午
 */
public class IsValidTest {

    public static boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        Map<Character,Character> map = new HashMap<Character,Character>(3){{
            put(']','[');
            put(')','(');
            put('}','{');

        }};
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //输入的是]、}、)
            if(map.containsKey(ch)){
                if(stack.isEmpty()|| !stack.peek().equals(map.get(ch))){
                    return false;
                }
                //匹配到了元素，则将括号及括号内的元素移除
                stack.pop();
            }else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
//        String str = "([)]";
        String str = "()[]{}";
        System.out.println(isValid(str));
    }
}
